﻿/*
============================================================================
文件名    ： Tools.cs
完全限定名： SSS.Common.Tools
作者      ： 陈存腾
创建时间  ： 2012-03-14
修改时间  ： 
版本      ：
说明      ： 基础辅助类
============================================================================
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace SSS.Common
{
    public class Tools
    {
        #region 设置页面不被缓存

        /// <summary>
        /// 设置页面不被缓存
        /// </summary>
        public static void SetPageNoCache()
        {
            HttpContext.Current.Response.Buffer = true;
            HttpContext.Current.Response.ExpiresAbsolute = System.DateTime.Now.AddSeconds(-1);
            HttpContext.Current.Response.Expires = 0;
            HttpContext.Current.Response.CacheControl = "no-cache";
            HttpContext.Current.Response.AddHeader("cache-control", "");
            HttpContext.Current.Response.AddHeader("Pragma", "No-Cache");
        }

        #endregion

        #region 页面跳转

        /// <summary>
        /// 页面跳转
        /// </summary>
        /// <param name="url">要跳转到的url地址</param>
        public void Redirect(string url)
        {
            Redirect(url, null);
        }

        /// <summary>
        /// 弹出信息,并跳转指定页面。
        /// </summary>
        /// <param name="url">要跳转到的url地址</param>
        /// <param name="message">提示信息</param>
        public static void Redirect(string url, string message)
        {
            string js;
            if (!String.IsNullOrWhiteSpace(message))
            {
                js = string.Format("<script language=javascript>alert('{0}');window.location.replace('{1}')</script>", message, url);
            }
            else
            {
                js = string.Format("<script language=javascript>window.location.replace('{1}')</script>", url);
            }
            HttpContext.Current.Response.Write(js);
            HttpContext.Current.Response.End();
        }

        #endregion

        /// <summary>
        /// 从System.Web.HttpRequest的Url中获取所调用的页面名称
        /// </summary>
        /// <returns>页面名称</returns>
        public static string GetPageName()
        {
            int start = 0;
            int end = 0;
            string Url = HttpContext.Current.Request.RawUrl;
            start = Url.LastIndexOf("/") + 1;
            end = Url.IndexOf("?");
            if (end <= 0)
            {
                return Url.Substring(start, Url.Length - start);
            }
            else
            {
                return Url.Substring(start, end - start);
            }
        }

        /// <summary>
        /// 获取当前请求页面相对于根目录的层级
        /// </summary>
        /// <returns></returns>
        public static string GetRelativeLevel()
        {
            string ApplicationPath = HttpContext.Current.Request.ApplicationPath;
            if (ApplicationPath.Trim() == "/")
            {
                ApplicationPath = "";
            }

            int i = ApplicationPath == "" ? 1 : 2;
            return "";//Nandasoft.Helper.NDHelperString.Repeat("../", Nandasoft.Helper.NDHelperString.RepeatTime(HttpContext.Current.Request.Path, "/") - i);
        }

    }
}
